import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
    meta: { requireAuth: false },
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/login'
  },
  // 注册
  {
    path: '/register/student',
    component: () => import('@/views/Register/student.vue')
  },
  {
    path: '/register/teacher',
    component: () => import('@/views/Register/teacher.vue')
  },
  // 提交作业
  {
    path: '/student/viewAssignment',
    component: () => import('@/views/Student/Lesson/Mine/viewAssignment.vue')
  },
  {
    path: '/student/myAssignment',
    component: () => import('@/views/Student/Lesson/Mine/myAssignment.vue')
  },
  // 成绩
  {
    path: '/course/gradeRecords/getByCourseId',
    component: () => import('@/views/CourseScore/index.vue')
  },
  {
    path: '/course/gradeRecords/getByCourseId/edit',
    component: () => import('@/views/CourseScore/modify.vue')
  },
  {
    path: '/course/gradeRecords/gradeRecorsd/addStudent',
    component: () => import('@/views/CourseScore/addStudent.vue')
  },
  // 作业
  {
    path: '/assignments',
    component: () => import('@/views/Assignment/index.vue')
  },
  {
    path: '/assignments/addAssignment',
    component: () => import('@/views/Assignment/addAssignment.vue')
  },
  // 组
  {
    path: '/groups/getByCourseId',
    component: () => import('@/views/Groups/index.vue')
  },
  // 提交作业
  {
    path: '/submittedAssignments',
    component: () => import('@/views/SubmittedAssignments/index.vue')
  },
  {
    path: '/seeSubmittedAssignments',
    component: () => import('@/views/SubmittedAssignments/seeSubmittedAssignments.vue')
  },
  // 组成员
  {
    path: '/groupMemberships/getByGroupId',
    component: () => import('@/views/GroupMember/index.vue')
  },
  // 题目
  {
    path: '/question',
    component: () => import('@/views/Question/index.vue')
  },
  {
    path: '/question/addQuestion',
    component: () => import('@/views/Question/addQuestion.vue')
  },


  {
    path: '/student/home',
    component: Layout,
    meta: {
      title: '学生主页',
      icon: 'el-icon-s-home',
      requireAuth: true,
      permission: 'student'
    },
    children: [
      {
        path: 'view',
        meta: {
          title: '查看消息',
          requireAuth: true,
          permission: 'student'
        },
        component: () => import('@/views/Student/Home/index.vue')
      },
    ]
  },

  {
    path: '/student/lesson',
    component: Layout,
    meta: {
      title: '学生课程',
      icon: 'el-icon-date',
      requireAuth: true,
      permission: 'student'
    },
    children: [
      {
        path: 'manage',
        meta: {
          title: '我的课程',
          requireAuth: true,
          permission: 'student'
        },
        component: () => import('@/views/Student/Lesson/Mine/index')
      },
      {
        path: 'timetable',
        meta: {
          title: '课程成绩',
          requireAuth: true,
          permission: 'student'
        },
        component: () => import('@/views/Student/Lesson/Achievement/index')
      },
    ]
  },

  {
    path: '/student',
    component: Layout,
    meta: {
      title: '信息管理',
      icon: 'el-icon-s-custom',
      requireAuth: true,
      permission: 'student'
    },
    children: [
      {
        path: 'information',
        meta: {
          title: '查看个人信息',
          requireAuth: true,
          permission: 'student'
        },
        component: () => import('@/views/Student/Personal/index.vue')
      },
      {
        path: 'modify',
        meta: {
          title: '修改个人信息',
          requireAuth: true,
          permission: 'student'
        },
        hidden: true,
        component: () => import('@/views/Student/Personal/modify.vue')
      },
    ]
  },




  {
    path: '/teacher/home',
    component: Layout,
    meta: {
      title: '老师主页',
      icon: 'el-icon-s-home',
      requireAuth: true,
      permission: 'teacher'
    },
    children: [
      {
        path: 'view',
        meta: {
          title: '查看消息',
          requireAuth: true,
          permission: 'teacher'
        },
        component: () => import('@/views/Teacher/Home/index.vue')
      },
    ]
  },
  {
    path: '/teacher/lesson',
    component: Layout,
    meta: {
      title: '老师课程',
      icon: 'el-icon-date',
      requireAuth: true,
      permission: 'teacher'
    },
    children: [
      // {
      //   path: 'timetable',
      //   meta: {
      //     title: '查看课表',
      //     requireAuth: true,
      //     permission: 'teacher'
      //   },
      //   component: () => import('@/views/Teacher/Lesson/Timetable/index')
      // },
      {
        path: 'manage',
        meta: {
          title: '管理课程',
          requireAuth: true,
          permission: 'teacher'
        },
        component: () => import('@/views/Teacher/Lesson/Manage/index')
      },
      {
        path: 'question',
        meta: {
          title: '管理题库',
          requireAuth: true,
          permission: 'teacher'
        },
        component: () => import('@/views/Question/index.vue')
      }
    ]
  },
  {
    path: '/teacher',
    component: Layout,
    meta: {
      title: '信息管理',
      icon: 'el-icon-s-custom',
      requireAuth: true,
      permission: 'teacher'
    },
    children: [
      {
        path: 'information',
        meta: {
          title: '查看个人信息',
          requireAuth: true,
          permission: 'teacher'
        },
        component: () => import('@/views/Teacher/Personal/index.vue')
      },
      {
        path: 'modify',
        meta: {
          title: '修改个人信息',
          requireAuth: true,
          permission: 'teacher'
        },
        hidden: true,
        component: () => import('@/views/Teacher/Personal/modify.vue')
      },
    ]
  },



  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'history',
  base: '/app', // 设置基础路径，避免与后端路由冲突
  scrollBehavior: () => ({ y: 0 }),
  routes: routes
})

const router = createRouter()

// 重置路由方法
export function resetRouter() {
  // 得到一个全新的router实例对象
  const newRouter = createRouter()
  // 使用新的路由记录覆盖掉老的路由记录
  router.matcher = newRouter.matcher
}


//全局前置守卫：初始化时执行、每次路由切换前执行
/*router.beforeEach((to, from, next) => {
  console.log('beforeEach->要跳转的路由:上一个路由', to, from)
  // 获取localStorage中的token,验证是否登录
  let token = localStorage.getItem('token')
  // 获取当前登录的用户类型
  let userRole = (localStorage.getItem('form') || '{}').replace(/'/g, '"')
    .replace(/&quot;/g, '"'); // 修正可能的JSON格式问题
  userRole = JSON.parse(userRole).accountType || null; // 确保accountType是有效的，否则设为null
  if (to.meta.requireAuth) { //判断当前路由是否需要进行权限控制
    if(isEmpty(token)){
      // 未登录,跳转到登录页
      Message.error('请先登录！')
      next('/login')
    }

    if (userRole === to.meta.permission) { //权限控制的具体规则
      next() //放行
    } else {
      Message.error('无权限查看！')
    }
  } else {
    next() //放行
  }
})*/

export default router
